<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentMemberGallery.class.php, $Author:  $ */


/** Component for showing all member pictures
 *
 * @version $Id: PhpFox_ComponentMemberGallery.class.php 368 2008-02-26 16:04:59Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentMemberGallery extends BaseComponent
{
    function process()
    {
        $iUserId = $this->getParam('userId');

		if ( !$iUserId )
		{
			return '';
		}

        $oGalleryItem = $this->_oModule->getItem('Gallery');
        /* $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Pager');

        $oSecurityService->checkPerm('gallery', array('read_only', 'full'));
        $oSecurityService->checkAccess('gallery');
        
        if ($iId = $oReq->getInt('makefeature'))
        {
        	if (App::isStaff())
            {
            	$oGalleryItem->load($iId);
            	$oGalleryItem->aData['feature'] = 1;            	
            	$oGalleryItem->update(array('feature'));
            }
            $oUrl->clearParams('makefeature');
            App::forward($oUrl->getUrl());
        }
        if ($iImageId = $oReq->getInt('delete'))
        {
            if (App::isStaff())
            {
                $oGalleryService->deleteItem($iImageId);
            }
            $oUrl->clearParams('delete');
            App::forward($oUrl->getUrl(), App::format('Mod_Gallery.info.image_deleted'), 'message', App::getPageName(), 'gallery');
        }
        if ($iId = $oReq->getInt('removefeature'))
        {
            if (App::isStaff())
            {
	            $oGalleryItem->load($iId);
	            $oGalleryItem->aData['feature'] = '';
	            $oGalleryItem->update(array('feature'));
            }
            $oUrl->clearParams('removefeature');
            App::forward($oUrl->getUrl());
        }        
        
        $sUser = $oReq->get('name');
        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('public.pagesize');
        $aCond = array();
        $aCond['user_id'] = '= "'. $iUserId .'"';
        $aCond['allow'] = '= ""';

        if (!App::isStaff())
        {
        	if ( $iUserId != App::getUser('id') )
        	{
        		$oSrvFriend = &App::getModuleService('Account', 'Friends');
        		$aCond['who_view'] = ($oSrvFriend->checkIsFriend($sUser, App::getUser('user')) ? '!= "2"' : '= ""');
        	}
        }

        $aItems = array();
        list($aImages, $iCnt) = $oGalleryItem->getList($aCond, $iPage, $iPageSize, 'time DESC', '', '', 'ma.id, ma.text, ma.feature');
        foreach($aImages as $sImage)
        {
        	$sImage['user'] = $sUser;
        	$aItems[] = $sImage;
        }

        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $oTpl->assign(array(
            'aPaging'   => ($iCnt) ? $oPager->getInfo($oUrl) : '',
            'aItems'    => $aItems,
            'iCnt'      => $iCnt,
            'iFrom'     => ($iCnt) ? ($iFrom + 1) : 0,
            'iTo'       => $iTo,
        	'sUser'     => $sUser,
            'sImageUrl' => App::getParam('gallery.pic.url'),
        ));
        $sHtml = $oTpl->fetch('MemberGallery.html');
        return $sHtml;
    }
}
?>